/****************************************************************************
 * Copyright 2009 Huascar A. Sanchez                                        *
 *                                                                          *
 * Licensed under the Apache License, Version 2.0 (the "License");          *
 * you may not use this file except in compliance with the License.         *
 * You may obtain a copy of the License at                                  *
 *                                                                          *
 *     http://www.apache.org/licenses/LICENSE-2.0                           *
 *                                                                          *
 * Unless required by applicable law or agreed to in writing, software      *
 * distributed under the License is distributed on an "AS IS" BASIS,        *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
 * See the License for the specific language governing permissions and      *
 * limitations under the License.                                           *
 ****************************************************************************/
package com.gotobject.filter.xml;

import static com.gotobject.filter.common.Aggregations.count;
import com.gotobject.filter.common.Predicates;
import static com.gotobject.filter.common.Predicates.attribute;
import com.gotobject.filter.spi.Xml;
import static com.gotobject.filter.XmlFilterQ.from;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Element;

import java.net.URL;

/**
 * @author huascar.sanchez@gmail.com (Huascar A. Sanchez)
 */
public class XmlFilterTest {
    private URL xmlPath;
    @Before
    public void setUp() throws Exception {
        xmlPath = XmlFilterTest.class.getResource("Person.xml");
    }


    @Test
    public void testXml_NoConditions_Descendants() throws Exception {
        final Iterable<Element> elements =  from(Xml.file(xmlPath))
                                            .descendants("person")
                                            .select();
        Assert.assertSame("2 ownership elements", 2, count(elements));
    }


    @Test
    public void testXml_WithConditions_Descendants() throws Exception {
        final Iterable<Element> elements =  from(Xml.file(xmlPath))
                                            .descendants("person")
                                            .select(attribute("since", "08/07/2009"));
        Assert.assertSame("1 ownership element", 1, count(elements));
    }    

    @Test
    public void testXml_WithConditions_Elements() throws Exception {
        final Iterable<Element> elements =  from(Xml.file(xmlPath))
                                            .elements("person")
                                            .select(attribute("age", "17"));
        Assert.assertSame("2 elements", 2, count(elements));
    }


    @Test
    public void testXml_NoConditions_Elements() throws Exception {
        final Iterable<Element> elements =  from(Xml.file(xmlPath))
                                            .elements("person")
                                            .select();
        Assert.assertSame("4 elements", 4, count(elements));
    }

    @Test
    public void testXml_NoConditions_Ancestors() throws Exception {
        final Iterable<Element> anc  =  from(Xml.file(xmlPath))
                                        .ancestors("ownership")
                                        .select();
        Assert.assertSame("2 elements", 2, count(anc));
    }

    @Test
    public void testXml_WithConditions_Ancestors() throws Exception {
        final Iterable<Element> anc  =  from(Xml.file(xmlPath))
                                        .ancestors("ownership")
                                        .select(attribute("name", "John"));
        Assert.assertSame("1 elements", 1, count(anc));        
    }

    @After
    public void tearDown() throws Exception {
        xmlPath = null;
    }
}
